package com.tomato.tv.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.tomato.tv.R
import com.tomato.tv.adapter.HistoryItemAdapter
import com.tomato.tv.entity.Channel
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity(), OnItemClickListener {

    private var historyItemAdapter: HistoryItemAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
        initData()
    }

    private fun initView() {
        recyclerView.layoutManager =
            LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
        historyItemAdapter = HistoryItemAdapter()
        recyclerView.adapter = historyItemAdapter
        historyItemAdapter!!.setOnItemClickListener(this)
    }

    private fun initData() {
        historyItemAdapter?.setList(initChannelList())
    }

    private fun initChannelList(): List<Channel>? {
        val list: ArrayList<Channel> = ArrayList()

        var channel = Channel(name = "Exoplayer", url = "null")
        list.add(channel)

        channel = Channel(name = "MediaPlayer", url = "null")
        list.add(channel)

        channel = Channel(name = "IJKPlayer", url = "null")
        list.add(channel)

        channel = Channel(name = "CCTV4", url = "null")
        list.add(channel)

        channel = Channel(name = "CCTV5", url = "null")
        list.add(channel)

        return list
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        when (position) {
            0 -> {
                startActivity(Intent(this, ExoPlayerActivity::class.java))
            }
            1 -> {
                startActivity(Intent(this, MediaPlayerActivity::class.java))
            }
            2-> {
                startActivity(Intent(this,IJKPlayerActivity::class.java))
            }
        }
    }
}